using Godot;
using System;

namespace Box;

public partial class CreateFileWindow : FileDialog
{
	public enum FileType {
		Task,
		Action,
	}

	public struct CreateSetting {
		public string Path;
		public string ProjectName;
	}

	[Export]
	public FileType Type {
		set {
			type = value;
			switch(Type) {
			case FileType.Task : {
				Title = "创建Task";
			}break;
			case FileType.Action : {
				Title = "创建Action";
			}break;
		}
		}

		get {
			return type;
		}
	}

	public FileType type;

	public string task_class_temp;
	public string action_class_temp;


	public override void _Ready()
	{
		Type = Type;
		FileSelected += _onCreate;

		FileAccess file = FileAccess.Open("res://source/process_tree_editor/create_file_window/task_class.temp",FileAccess.ModeFlags.Read);
		task_class_temp = file.GetAsText();
		file.Close();
		file = FileAccess.Open("res://source/process_tree_editor/create_file_window/action_class.temp",FileAccess.ModeFlags.Read);
		action_class_temp = file.GetAsText(); 
		file.Close();
	}

	public void CreateTaskFile(string class_file_full_path,string name) {
		string class_namespace_name = ProcessTreeEditor.ProjectName;
		string class_code = string.Format(task_class_temp,class_namespace_name,
		name,
		class_namespace_name,
		class_file_full_path,
		name);
		FileAccess file = FileAccess.Open(class_file_full_path,FileAccess.ModeFlags.WriteRead);

		ItemSelectTree.TryCreateTokenFile($"{class_namespace_name}.{name}","task",name,class_namespace_name,class_file_full_path);
		file.StoreString(class_code);
		file.Flush();
		file.Close();
	}

	public void CreateActionFile(string class_file_full_path,string name) {
		string class_namespace_name = ProcessTreeEditor.ProjectName;
		string class_code = string.Format(action_class_temp,class_namespace_name,
		name,
		class_namespace_name,
		class_file_full_path,
		name);
		FileAccess file = FileAccess.Open(class_file_full_path,FileAccess.ModeFlags.WriteRead);

		ItemSelectTree.TryCreateTokenFile($"{class_namespace_name}.{name}","action",name,class_namespace_name,class_file_full_path);
		file.StoreString(class_code);
		file.Flush();
		file.Close();
	}

	public void CreateProject(string name) {
		
	}

	public void _onCreate(string full_path) {
		string class_name = this.CurrentFile.Substr(0,CurrentFile.Length - 3);
		switch(Type) {
			case FileType.Task : {
				CreateTaskFile(full_path,class_name);
			}break;
			case FileType.Action : {
				CreateActionFile(full_path,class_name);
			}break;
		}
	}

	
	public override void _Process(double delta)
	{

	}
}
